<?php
require_once(dirname(__FILE__) . '/app.php');

$id = abs(intval($_GET['id']));
$team = Table::FetchForce('team', $id);
if (!$id || !$team ) {
	redirect( WEB_ROOT . '/team/index.php');
}

/* refer */
if ($_rid = abs(intval($_GET['r']))) { 
	if($_rid) cookieset('_rid', abs(intval($_GET['r'])));
	redirect( WEB_ROOT . "/team.php?id={$id}");
}

$pagetitle = $team['title'];

/* discount */
//$discount_price = $team['market_price'] - $team['team_price'];
//$discount_rate = $team['team_price']/$team['market_price']*10;
/* End */

//$left = array();

/* left time */
$now = time();
$diff_time = $left_time = $team['end_time']-$now;
$left_day = floor($diff_time/86400);
$left_time = $left_time % 86400;
$left_hour = floor($left_time/3600);
$left_time = $left_time % 3600;
$left_minute = floor($left_time/60);
$left_time = $left_time % 60;
/* End */

/* progress bar size */
//$bar_size = ceil(190*($team['now_number']/$team['max_number']));
//$bar_offset = ceil(5*($team['now_number']/$team['max_number']));
/* End */

/* other teams */
$city = Table::Fetch('category', $team['city_id']);
if(!$city) { $city = array('id' => 0, 'name' => '全部', ); }
// 侧栏显示几个其他活动
//if ( abs(intval($INI['system']['sideteam'])) ) {
	$oc = array( 
			'city_id' => $city['id'], 
			"id <> {$id}",
			"begin_time < {$now}",
			"end_time > {$now}",
			);
	$others = DB::LimitQuery('team', array(
				'condition' => $oc,
				'order' => 'ORDER BY id DESC',
				'size' => abs(intval($INI['system']['sideteam'])),
				));
//}
/* End */

$team['state'] = team_state($team);

/* your order */
if ($login_user_id && 0==$team['close_time']) {
	$order = DB::LimitQuery('order', array(
		'condition' => array(
			'team_id' => $id,
			'user_id' => $login_user_id,
			'state' => 'unpay',
		),
		'one' => true,
	));
}
/* end order */

/* get tatics */
$min_discount = 10;
$max_discount = 0;
$tatics = explode('|', $team['tatics']);
foreach($tatics as $sid=>$one){
	$tatics[$sid] = explode(',', $one);
	$discountTmp = explode('-', $tatics[$sid][2]);
	if(($user_network_weights['total'] >= $tatics[$sid][0])){
		if($tatics[$sid][1] == 0){
			$team['discount_rate'] = $tatics[$sid][2];
		} elseif($user_network_weights['total'] < $tatics[$sid][1]){
			if($user_network_weights['total'] == 0){
				$team['discount_rate'] = $discountTmp[1];
			} else{
				// 低折扣+（高折扣-低折扣）/（高影响力-低影响力）*（个人影响力-低影响力）
				$team['discount_rate'] = round($discountTmp[0] + (($discountTmp[1] - $discountTmp[0])/($tatics[$sid][1] - $tatics[$sid][0]))*($user_network_weights['total'] - $tatics[$sid][0]), 1);
			}
		}
	}
	if($min_discount > $discountTmp[0]){
		$min_discount = $discountTmp[0];
	}
	if($max_discount < $discountTmp[1]){
		$max_discount = $discountTmp[1];
	}
}
$team['min_discount'] = $min_discount;
$team['max_discount'] = $max_discount;
/* End */

/* partner info */
$partner = Table::Fetch('partner', $team['partner_id']);
/* End */

include template('team_view');
//redirect($partner['homepage']);